package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Customer;
import io.wins.vertx.entity.CustomerAddress;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.CustomerService;

public class CustomerServiceImpl extends BaseService implements CustomerService {
    public CustomerServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public CustomerService delCustAddress(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "delete from ds_customer_address where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public CustomerService listCustAddress(final String custId, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_customer_address where parent_id = ?")
            .put(PARAMS, new JsonArray().add(custId));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public CustomerService saveCustAddress(final CustomerAddress ca, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_customer_address set receiver=?, phone=?, postcode=?, area_code=?, area_name=?, detail=? where parent_id = ?")
            .put(PARAMS, new JsonArray()
                .add(ca.getReceiver())
                .add(ca.getPhone())
                .add(ca.getPostcode())
                .add(ca.getAreaCode())
                .add(ca.getAreaName())
                .add(ca.getParentId()));
        this.updateSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public CustomerService createCustAddress(final CustomerAddress ca, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_customer_address (id, parent_id, receiver, phone, postcode, is_default, area_code, area_name, detail) VALUES (?,?,?,?,?,?,?,?,?)")
            .put(PARAMS, new JsonArray()
                .add(ca.getId())
                .add(ca.getParentId())
                .add(ca.getReceiver())
                .add(ca.getPhone())
                .add(ca.getPostcode())
                .add(ca.isDefault())
                .add(ca.getAreaCode())
                .add(ca.getAreaName())
                .add(ca.getDetail()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public CustomerService setDefaultAddress(final String id, final String custId, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject();
        return this;
    }

    @Override
    public void loadCustomer(final String id, final Message<JsonObject> tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_customer where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
    }

    @Override
    public void delCustomer(final String id, final Message<JsonObject> tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "delete from ds_customer where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
    }

    @Override
    public void updateCustomer(final Customer c, final Message<JsonObject> tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_customer set nickname=?, real_name=?, sex=?, phone=?, email=?, birthday=?, image=?, description=?, updated_on = UNIX_TIMESTAMP()")
            .put(PARAMS, new JsonArray()
                .add(c.getNickname())
                .add(c.getRealName())
                .add(c.isSex())
                .add(c.getPhone())
                .add(c.getEmail())
                .add(c.getBirthday())
                .add(c.getImage())
                .add(c.getDescription())
            );
        this.updateSql(sqlMes, this.resultUpdate(tMessage, "Error"));
    }

    @Override
    public void listCustomer(final Message<JsonObject> tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_customer order by updated_on desc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
    }
}
